#!/bin/bash

CUR_DIR="$( cd "$( dirname "$(readlink -f "$0")" )" && pwd )"
source $CUR_DIR/cluster_IP.conf

function config_peer_IP() {
    i=1
    for node in ${FABRIC_PEER_IPs[@]}
    do
        IP_in_hosts=$(grep peer0.org${i}.demo.com /etc/hosts | awk '{print $1}')
        if [ "$IP_in_hosts" == "" ]; then
            echo "$node peer0.org${i}.demo.com" | sudo tee -a /etc/hosts
        elif [ "$IP_in_hosts" != "$node" ]; then
            sudo sed -i "s/.*peer0.org${i}.demo.com/$node peer0.org${i}.demo.com/g" /etc/hosts
        fi
        let i=$i+1
    done
}

function config_orderer_IP() {
    i=1
    for node in ${FABRIC_ORDERER_IPs[@]}
    do
        IP_in_hosts=$(grep orderer${i}.demo.com /etc/hosts | awk '{print $1}')
        if [ "$IP_in_hosts" == "" ]; then
            echo "$node orderer${i}.demo.com" | sudo tee -a /etc/hosts
        elif [ "$IP_in_hosts" != "$node" ]; then
            sudo sed -i "s/.*orderer${i}.demo.com/$node orderer${i}.demo.com/g" /etc/hosts
        fi
        let i=$i+1
    done
}

function config_ceph_IP () {
    i=1
    for node in ${CEPH_IPs[@]}
    do
        IP_in_hosts=$(grep peer0.org${i}.demo.com /etc/hosts | awk '{print $1}')
        if [ "$IP_in_hosts" == "" ]; then
            echo "$node peer0.org${i}.demo.com" | sudo tee -a /etc/hosts
        elif [ "$IP_in_hosts" != "$node" ]; then
            sudo sed -i "s/.*peer0.org${i}.demo.com/$node peer0.org${i}.demo.com/g" /etc/hosts
        fi
        let i=$i+1
    done
}

config_orderer_IP
config_peer_IP

# config_ceph_IP

